//
//  CourseDetailsCatalogView.m
//  ZFPlayer_Example
//
//  Created by 洪欣 on 2019/1/4.
//  Copyright © 2019年 紫枫. All rights reserved.
//

#import "CourseDetailsCatalogView.h"
#import "CourseDetailsCatalogCollectionViewCell.h"

@interface CourseDetailsCatalogView ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *typeView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;
@property (strong, nonatomic) UIView *lineView;
@property (strong, nonatomic) UIButton *tempBtn;
@property (strong, nonatomic) NSMutableArray *buttonArray;
@property (strong, nonatomic) CourseDetailsCourseHourOutputDTODataModel *currentSelcetModel;
@property (strong, nonatomic) CourseDetailsCatalogCollectionViewCell *currentSelcetCell;
@property (strong, nonatomic) CourseDetailsCourseHourOutputDTODataModel *playingModel;
@end

@implementation CourseDetailsCatalogView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 130);
    
    self.collectionView.pagingEnabled = YES;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CourseDetailsCatalogCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:@"CellId"];
    
    self.typeView.showsVerticalScrollIndicator = NO;
    self.typeView.showsHorizontalScrollIndicator = NO;
    
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 130, 2)];
    self.lineView.backgroundColor = [UIColor orangeColor];
    
    
    [self.typeView addSubview:self.lineView];
    
}
- (void)setModel:(CourseDetailsDataModel *)model {
    if (_model) {
        return;
    }
    _model = model;
    
    for (UIView *view in self.buttonArray) {
        [view removeFromSuperview];
    }
    self.buttonArray = [NSMutableArray array];
    for (int i = 0 ; i < model.courseOutputDTOList.count ; i++) {
        CourseDetailsCourseOutputDTODataModel *dataModel = model.courseOutputDTOList[i];
        NSString *title = dataModel.courseName;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:102/255.f green:102/255.f blue:102/255.f alpha:1] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:102/255.f green:102/255.f blue:102/255.f alpha:1] forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(didbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(15*ScaleX * (i + 1) + (130 * i)*ScaleX, 10*ScaleX, 130*ScaleX, 30*ScaleX);
        [self.typeView addSubview:button];
//        self.typeView.backgroundColor = [UIColor redColor];
        if (i == 0) {
            self.collectionViewHeight.constant = dataModel.courseHourOutputDTOList.count * 65.f;
            self.flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, self.collectionViewHeight.constant);
            [self layoutIfNeeded];
            button.enabled = NO;
            self.tempBtn = button;
            self.lineView.center = CGPointMake(button.center.x, self.lineView.center.y);
        }
        if (i == model.courseOutputDTOList.count - 1) {
            self.typeView.contentSize = CGSizeMake(CGRectGetMaxX(button.frame) + 15*ScaleX, 40*ScaleX);
        }
        [self.buttonArray addObject:button];
    }
    CourseDetailsCourseOutputDTODataModel *cellModel = self.model.courseOutputDTOList.firstObject;
    CourseDetailsCourseHourOutputDTODataModel *subModel = cellModel.courseHourOutputDTOList.firstObject;
    subModel.selected = YES;
    self.currentSelcetModel = subModel;
    [self.collectionView reloadData];
}

- (void)didbuttonClick:(UIButton *)button {
    if (button == self.tempBtn) {
        return;
    }
    self.tempBtn.enabled = YES;
    button.enabled = NO;
    self.tempBtn = button;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.center = CGPointMake(button.center.x, self.lineView.center.y);
    }];
    
//    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:button.tag inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    [UIView animateWithDuration:0.3 animations:^{
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat offsetx = width*button.tag;
        self.collectionView.contentOffset = CGPointMake(offsetx, 0);
    }];
    
    CourseDetailsCourseOutputDTODataModel *dataModel = self.model.courseOutputDTOList[button.tag];
    [UIView animateWithDuration:0.25 animations:^{
        self.collectionViewHeight.constant = dataModel.courseHourOutputDTOList.count * 65.f;
        [self.superview layoutIfNeeded];
    }];
    
    self.flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, dataModel.courseHourOutputDTOList.count * 65.f);
    [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:button.tag inSection:0]]];
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    if (self.typeView.contentSize.width < screenWidth) {
        return;
    }
    if (button.frame.origin.x + button.frame.size.width / 2  < screenWidth / 2) {
        [self.typeView setContentOffset:CGPointMake(0, 0) animated:YES];
        return;
    }
    if (button.frame.origin.x + button.frame.size.width / 2 > self.typeView.contentSize.width - screenWidth / 2) {
        [self.typeView setContentOffset:CGPointMake(self.typeView.contentSize.width - screenWidth, 0) animated:YES];
        return;
    }
    CGRect rect = [button.superview convertRect:button.frame toView:[UIApplication sharedApplication].keyWindow];
    CGFloat centerX = rect.origin.x + rect.size.width / 2;
    CGFloat offsetX = 0;
    if (centerX < screenWidth / 2) {
        offsetX = screenWidth / 2 - centerX;
        [self.typeView setContentOffset:CGPointMake(self.typeView.contentOffset.x - offsetX, 0) animated:YES];
    }else if (centerX > screenWidth / 2){
        offsetX = centerX - screenWidth / 2;
        [self.typeView setContentOffset:CGPointMake(self.typeView.contentOffset.x + offsetX, 0) animated:YES];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.courseOutputDTOList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CourseDetailsCatalogCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellId" forIndexPath:indexPath];
    cell.model = self.model.courseOutputDTOList[indexPath.item];
    
    __weak typeof(self) weakSelf = self;
    cell.selectTableViewCellBlock = ^(CourseDetailsCourseHourOutputDTODataModel * _Nonnull model, CourseDetailsCatalogCollectionViewCell * _Nonnull myCell) {
        //区别选中
        //这里是控制重复点击  
        if (weakSelf.playingModel == model) {
            return;
        }
        if (weakSelf.playingModel) {
            weakSelf.currentSelcetModel.selected = NO;
            [weakSelf.currentSelcetCell resetState];
        }
        
        
        
        weakSelf.currentSelcetModel = model;
        weakSelf.currentSelcetCell = myCell;
        weakSelf.playingModel = model;
        if (weakSelf.selectTableViewCellBlock) {
            weakSelf.selectTableViewCellBlock(model);
        }
    };
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.collectionView) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        CGFloat offsetx = self.collectionView.contentOffset.x;
        NSInteger currentIndex = (offsetx + width * 0.5) / width;
        if (currentIndex < 0) currentIndex = 0;
        if (currentIndex > 4) currentIndex = 4;
        [self didbuttonClick:self.buttonArray[currentIndex]];
    }
}

@end
